import entity.Account;
import entity.LoginUser;
import entity.User;
import entity.vo.UserDetailVo;
import org.springframework.beans.BeanUtils;
import service.AccountService;
import service.UserService;
import service.impl.AccountServiceImpl;
import service.impl.UserServiceImpl;
import utils.InfoUtils;
import utils.NumUtils;
import utils.ViewUtils;

import java.util.*;


/**
 * @program: FinalExam
 * @description:主函数
 * @create: 2021-12-14 16:57
 * @Date 2021/12/14
 **/

public class enterMainFunction {
    private static LoginUser loginUser = new LoginUser();
    private static Integer leftTime = 3;
    private static UserService userService = new UserServiceImpl();
    private static AccountService accountService = new AccountServiceImpl();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
            choiceMenu();
    }

    public static void choiceMenu(){
        ViewUtils.choiceMenu();
        Integer choice = NumUtils.backChoiceInfo(scanner,1,3);
        switch (choice){
            case 1:
                loginMenu(choice);
                enterAdminMenu();
                break;
            case 2:
                loginMenu(choice);
                enterUserMenu();
                break;
            case 3:
                System.out.println("退出成功");
        }
    }
    public static void enterAdminMenu(){
        ViewUtils.adminMenu();
        Integer choice = NumUtils.backChoiceInfo(scanner, 1, 6);
        switch (choice){
            case 1:
                showUsersInfo();
                enterAdminMenu();
                break;
            case 2:
                addUser();
                break;
            case 3:
                deleteUser();
                break;
            case 4:
                modifyUser();
                break;
            case 5:
                queryById();
                break;
            case 6:
                exit();
                break;
        }
    }

    public static void addUser(){
        System.out.print("请输入的姓名:");
        String realname = scanner.next();
        System.out.print("请输入你的密码:");
        String password = scanner.next();
        System.out.print("请再次输入你的密码:");
        String confirmPassowrd = scanner.next();
        if (password.equals(confirmPassowrd)) {
            // 增加用户
            User user = new User();
            user.setRealname(realname);
            user.setPassword(password);
            String username =  userService.addUser(user);
            System.out.println("添加成功，你的卡号为："+username+"，请牢记");

            enterAdminMenu();
        }else{
            System.out.println("两次输入的密码不一致");
            enterAdminMenu();
        }
    }

    public static void showUsersInfo(){
        List<User> list = userService.findAllUserInfo();
        List<User> filterInfo = InfoUtils.filterInfo(list);
        for (User user : filterInfo) {
            System.out.println(user);
        }

    }
    public static void enterUserMenu(){
        ViewUtils.userMenu();
       Integer choice = NumUtils.backChoiceInfo(scanner, 1, 5);
        switch (choice){
            case 1:
                queryMyBalance();
                break;
            case 2:
                transferAccounts();
                break;
            case 3:
                saveMoney();
                break;
            case 4:
                modifyPassword();
                break;
            case 5:
                exit();
                break;
        }

    }

    private static void transferAccounts() {
        Account account = accountService.QueryMyBalance(loginUser.getId());
        System.out.println("你当前的余额为:"+account.getBalance());
        if(account.getBalance() % 100 != 0){
            System.out.println("转账应为100的倍数");
            enterUserMenu();
        }else{
            ViewUtils.showMoney();
            Integer times = NumUtils.backChoiceInfo(scanner, 1, 3);
            System.out.print("请输入你要转账的账户:");
            String toUsername = scanner.next();
            int i = NumUtils.intPutNum(toUsername);
            if(i==-1){
                enterUserMenu();
            }
            Account acc = accountService.QueryMyBalanceByUsername(toUsername);
            if(acc==null){
                System.out.println("查无此人");
            }else {

                double money = NumUtils.getMoney(times);
                if(account.getBalance() - money <=0){
                    System.out.println("余额不足");
                    enterUserMenu();
                }else{
                    accountService.transferAccountsNyUsername(loginUser.getId(),money,toUsername);
                    enterUserMenu();
                }
            }
        }
    }

    private static void queryMyBalance() {
        Account account = accountService.QueryMyBalance(loginUser.getId());
        System.out.println("你的余额为："+account.getBalance());
        enterUserMenu();
    }

    public static boolean loginMenu(Integer choice){
        if(leftTime == 0){
            try {
                System.out.println("冻结账号，3秒钟后恢复");
                Thread.sleep(3000);
                leftTime = 3;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print("请输入你的账号:");
        String username = scanner.next();
        System.out.print("请输入你的密码:");
        String password = scanner.next();
        int i = NumUtils.intPutNum(username);
        if(i==-1 || username.toCharArray().length != 6){
            System.out.println("非法输入，请重新出入（账户由6为数字组成）");
            return loginMenu(choice);
        }else{
            User user = new User();
            switch (choice){
                case 1:
                    user = userService.adminLogin(username, password);

                    if(user == null)
                        choiceMenu();
                    break;
                case 2:
                    user = userService.userLogin(username, password);
                    if(user == null)
                        choiceMenu();
                    break;
            }
            if(user!=null){
                BeanUtils.copyProperties(user,loginUser);
                return true;
            }else{
                leftTime --;
                System.out.println("密码输入错误，还剩"+leftTime+"次机会");
                loginMenu(choice);
            }
        }

        return false;
    }
    public static void saveMoney(){
        System.out.println("请输入你要存款的金额");
        String money = scanner.next();
        Double totalMoney = null;
        if(Integer.parseInt(money) * 1.0  % 100 != 0 ){
            System.out.println("存款金额必须为100的倍数");
            enterUserMenu();
        }else{
            int i = NumUtils.intPutNum(money);
            if(i==-1){
                enterUserMenu();
            }else{
                // 存款
                Account account = accountService.QueryMyBalance(loginUser.getId());
                totalMoney = Integer.parseInt(money) * 1.0;
                accountService.saveMoney(account.getBalance() + totalMoney, loginUser.getId());
            }
        }
    }
    public static void modifyPassword(){
        String id = loginUser.getId();
        User user = userService.findById(id);
        System.out.println("请输入原密码");
        String password = scanner.next();
        if(!password.equals(user.getPassword())){
            System.out.println("原密码输入错误");
            enterUserMenu();
        }else{
            System.out.println("密码修改为:");
            String firstInput = scanner.next();
            System.out.println("请确认密码为:");
            String secondInput = scanner.next();
            if(!firstInput.equals(secondInput)){
                System.out.println("两次输入有误");
                enterUserMenu();
            }else{
                userService.modifyPassword(id,firstInput);
                System.out.println("修改成功");
                loginUser = null;
                choiceMenu();
            }
        }
    }
    public static void deleteUser(){
        showUsersInfo();
        System.out.print("请输入你要删除的序号:");
        String choice = scanner.next();
        int i = NumUtils.intPutNum(choice);
        if(i==-1){
            System.out.println("请正确输入");
            enterAdminMenu();
        }else{
            User res = userService.findById(choice);
            if(res!=null){
                userService.deleteUserById(choice);
                accountService.deleteAccount(choice);
                System.out.println("删除成功");
                enterAdminMenu();
            }
        }
    }
    public static void modifyUser(){
        // TODO
        System.out.println("权限不够，充值即可开启升级为超级管理员");
        enterAdminMenu();
    }
    public static void queryById(){
        showUsersInfo();
        System.out.print("请输入账户：");
        String input = scanner.next();
        int choice = NumUtils.intPutNum(input);
        if(choice!=-1){
            User user = userService.findById(input);
            Account account = accountService.QueryMyBalance(input);
            UserDetailVo userDetailVo = new UserDetailVo();
            BeanUtils.copyProperties(user,userDetailVo);
            BeanUtils.copyProperties(account,userDetailVo);
            userDetailVo.setCreate_time(user.getGmt_create());
            System.out.println(userDetailVo);
            enterAdminMenu();
        }else{
            System.out.println("输入有误");
            queryById();
        }
    }
    public static void exit(){
        System.out.println("退出成功，回到主页面");
        choiceMenu();
    }
}
